/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.MapLine;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Shop;
import com.rzico.basics.model.MapPoint;
import com.rzico.basics.model.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.ShopMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <pre>
 * 会员业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ShopService extends BaseServiceImpl<Shop, String> {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public BaseMapper<Shop, String> getMapper() {
        return shopMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object [] ids) {
        int rw = 0;
        for (Object id:ids) {
            Shop shop = selectByPrimaryKey(id);
            shop.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(shop);
        }
        return rw;
    }

    //查找离我最近的门店
    public Shop findByPoint(Enterprise enterprise,Double lat,Double lng) {

        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",enterprise.getId());
        params.put("sortField","id");
        params.put("sortType","asc");

        MapPoint point = new MapPoint();
        point.setLat(lat);
        point.setLng(lng);

        Shop mshop = null;
        Double d = 0d;

        List<Shop> shopList = shopMapper.selectList(params);
        if (lat==0 && lng==0 && shopList.size()>0) {
            return shopList.get(0);
        }
        for (Shop shop:shopList) {
            //在配送范围，判断离谁近
            Double dt = 9999999d;
            if (shop.getLat()==null) {
                shop.setLat(0d);
            }
            if (shop.getLng()==null) {
                shop.setLng(0d);
            }
            if (shop.getLat()>0 && shop.getLat()>0) {
                MapPoint shopPoint = new MapPoint();
                shopPoint.setLat(shop.getLat());
                shopPoint.setLng(shop.getLng());
                dt = point.distatce(shopPoint);
            }
            if (d.compareTo(0d)==0) {
                d = dt;
                mshop = shop;
            } else {
                if (d.compareTo(dt)>0) {
                    d = dt;
                    mshop = shop;
                }
            }
        }
        return mshop;

    }


    //查找离我最近的门店
    public Shop findByISV(Enterprise enterprise,Double lat,Double lng) {

        Map<String,Object> params = new HashMap<>();
        params.put("isvId",enterprise.getMchId());

        MapPoint point = new MapPoint();
        point.setLat(lat);
        point.setLng(lng);

        Shop mshop = null;
        Double d = 0d;

        List<Shop> shopList = shopMapper.selectList(params);
        for (Shop shop:shopList) {
            //在配送范围，判断离谁近
            Double dt = 9999999d;
            if (shop.getLat()==null) {
                shop.setLat(0d);
            }
            if (shop.getLng()==null) {
                shop.setLng(0d);
            }
            if (shop.getLat()>0 && shop.getLat()>0) {
                MapPoint shopPoint = new MapPoint();
                shopPoint.setLat(shop.getLat());
                shopPoint.setLng(shop.getLng());
                dt = point.distatce(shopPoint);
            }
            if (d.compareTo(0d)==0) {
                d = dt;
                mshop = shop;
            } else {
                if (d.compareTo(dt)>0) {
                    d = dt;
                    mshop = shop;
                }
            }
        }
        return mshop;

    }

    public List<ShopVo> selectLbsList(Map<String, Object> params,Double lat,Double lng) {

        List<ShopVo> shopList = shopMapper.selectLbsList(params);
        if (lat!=null && lng !=null && lat>0 && lng>0) {
            for (ShopVo shopVo : shopList) {
                if (shopVo.getLat() != null && shopVo.getLng() != null) {
                    MapPoint point = new MapPoint();
                    point.setLng(lng);
                    point.setLat(lat);

                    MapPoint shopPoint = new MapPoint();
                    shopPoint.setLat(shopVo.getLat());
                    shopPoint.setLng(shopVo.getLng());

                    Double d = point.distatce(shopPoint);
                    shopVo.setDistance(d);
                }
            }
            //开始排序
            Collections.sort(shopList, new Comparator<ShopVo>() {
                public int compare(ShopVo n1, ShopVo n2) {
                    if (n1.getDistance()==null) {
                        return -1;
                    }
                    if (n2.getDistance()==null) {
                        return 1;
                    }
                    return n1.getDistance().compareTo(n2.getDistance());
                }
            });
        }

        return shopList;

    }


    /**
     * 检查用户是否拥有某个门店的权限
     * @param userId
     * @param shopId
     * @return
     */
    public Boolean checkShopPower(String userId, Long shopId){
        boolean hasPower = false;
        Map<String, Object> params = new HashMap<>();
        params.put("shopScopeId", userId);
        List<Shop> shopList = shopMapper.selectList(params);
        if (null != shopList && shopList.size() > 0){
            for (Shop shop : shopList){
                if (shop.getId().equals(shopId)){
                    hasPower = true;
                    break;
                }
            }
        }
        return hasPower;
    }


}
